package com.example.qt.ptplatform.controller;

import com.example.qt.ptplatform.service.CommentImageService;
import com.example.qt.ptplatform.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/commentImage")
public class CommentImageController {

    @Autowired
    private CommentImageService commentImageService;

    @PostMapping("/upload")
    public JsonResult uploadCommentImage(
            @RequestParam("torrentId") Long torrentId,
            @RequestParam("userId") Long userId,
            @RequestParam("commentId") Integer commentId,
            @RequestParam("image") MultipartFile imageFile) {

        try {
            if (torrentId == null || userId == null || commentId == null) {
                return JsonResult.error("参数不能为空");
            }

            if (imageFile == null || imageFile.isEmpty()) {
                return JsonResult.error("图片不能为空");
            }

            boolean result = commentImageService.saveCommentImage(torrentId, userId, commentId, imageFile);

            return result ? JsonResult.success("图片上传成功") : JsonResult.error("图片上传失败");
        } catch (Exception e) {
            return JsonResult.error("图片上传失败: " + e.getMessage());
        }
    }
}